﻿using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Serialization;

namespace Order
{
    [Serializable]
    public class Program
    {
        static List<Goods> goods = new List<Goods>();
        static OrderService service = new OrderService();
        static List<OrderDetails> orderDetailsList=new List<OrderDetails>();
        

        [Serializable]
        public class Order
        {
            public Goods good;
            public int value;
            public int orderNumber;
            public Order(Goods good, int value, int ordernumber)
            {
                this.good = good;
                this.value = value;
                this.orderNumber = ordernumber;
            }
            public override string ToString()
            {
                return $"货物:{this.good.goodsName}\n金额:{this.value}";
            }
            public static void addGoods(string goodsId, string goodsName, string goodsType, int price) {
                goods.Add(new Goods(goodsId, goodsName, goodsType, price));
            }

        }
        [Serializable]
        public class OrderDetails
        {
            public List<Goods> goodsOrdered = new List<Goods>();
            public int orderID { get; set; }
            public string shop;
            public int goodsNumber;
            public string clientName;
            public Goods goods;
            public int price { get; set; }
            public OrderDetails(int orderID, string shop, int goodsNumber, string clientName, Goods goods)
            {
                this.orderID = orderID;
                this.shop = shop;
                this.goodsNumber = goodsNumber;
                this.clientName =clientName;
                this.goods = goods;
                this.price = goods.price * goodsNumber;
            }
            public OrderDetails() { }
            public override string ToString()
            {
                //string rst="";
                string rst ="客户名： " +clientName;
                rst += " 订单ID: " + orderID.ToString() +" 商品数量： "+goodsNumber+ " ";
                rst += goods.ToString();
                rst += $"总价:{price}";
                rst += "-----------------------------------";
                return rst;
            }
            public override bool Equals(object obj)
            {
                OrderDetails orderDetails = obj as OrderDetails;
                return orderDetails != null && orderDetails.orderID == orderID;
            }
            public override int GetHashCode()
            {
                return orderID;
            }
            public void addGood(int i)
            {
                if (i > 0 && i < 8)
                {
                    goodsOrdered.Add(Program.goods[i]);
                }
                else
                {
                    Console.WriteLine($"错误ID: 000{i}");
                }
            }
        }

        [Serializable]
        public class OrderService
        {
            Dictionary<int, OrderDetails> OrderList = new Dictionary<int, OrderDetails>();
            
            public void sort(int s)
            {
                if (s == 00)
                {
                    var query = from n in OrderList
                                orderby n.Value.orderID ascending
                                select n;
                }
                if (s == 01)
                {
                    var query = from n in OrderList.OrderByDescending(n => n.Value.orderID)
                                select n;
                }
                if (s == 10)
                {
                    var query = from n in OrderList
                                orderby n.Value.price ascending
                                select n;
                }
                if (s == 11)
                {
                    var query = from n in OrderList.OrderByDescending(n => n.Value.price)
                                select n;
                }
            }


            public bool findOrder(int i,int id)
            {
                if (i == 0)
                {
                    var query = from order in OrderList
                                where order.Value.orderID == id
                                orderby order.Value.price
                                select order;

                    if (query == null)
                    {
                        return false;
                    }
                    else
                    {
                        foreach (var n in query)
                        {
                            Console.WriteLine($"序号：{n.Key},订单号：{n.Value.orderID}，货品名称：{n.Value.goods.goodsName}，总价：{n.Value.price}，客户：{n.Value.clientName}");

                        }
                        return true;
                    }
                }
                else {
                    var query = from order in OrderList
                                where order.Value.price == id
                                orderby order.Value.price
                                select order;

                    if (query == null)
                    {
                        return false;
                    }
                    else
                    {
                        foreach (var n in query)
                        {
                            Console.WriteLine($"序号：{n.Key},订单号：{n.Value.orderID}，货品名称：{n.Value.goods.goodsName}，总价：{n.Value.price}，客户：{n.Value.clientName}");

                        }
                        return true;
                    }
                }
            }
            public void findOrder(int i, string Name)
            {
                if (i == 0)
                {
                    var query = from order in OrderList
                                where order.Value.goods.goodsName == Name
                                orderby order.Value.price descending
                                select order;
                    foreach (var n in query)
                    {
                        Console.WriteLine($"序号：{n.Key},订单号：{n.Value.orderID}，货品名称：{n.Value.goods.goodsName}，总价：{n.Value.price}，客户：{n.Value.clientName}");
                    }
                }
                if (i == 1)
                {
                    var query = from order in OrderList
                                where order.Value.clientName == Name
                                orderby order.Value.price descending
                                select order;
                    foreach (var n in query)
                    {
                        Console.WriteLine($"序号：{n.Key},订单号：{n.Value.orderID}，货品名称：{n.Value.goods.goodsName}，总价：{n.Value.price}，客户：{n.Value.clientName}");
                    }
                }
            }
            public void addOrder(int id,OrderDetails order)
            {
                foreach (OrderDetails thisorder in OrderList.Values)
                {
                    if (thisorder.Equals(order))
                    {
                        Console.WriteLine("订单已存在!");
                        return;
                    }
                }
                OrderList.Add(id, order);
            }
            public void removeOrder(int id)
            {
                if (OrderList.ContainsKey(id))
                {
                    OrderList.Remove(id);
                }
                else
                {
                    Console.WriteLine("无此商品ID");
                }
            }

            public void modifyOrder(int orderID) {
                
                Console.WriteLine($"{service.OrderList[orderID].ToString()}");
                Console.WriteLine("你可以对你的订单进行修改");
                Console.WriteLine("1.增加商品  2.减少商品  3.退出");
                Console.Write("你的选择: ");
                int choice = int.Parse(Console.ReadLine());
                if ((choice == 1||choice==2)&& findOrder(0,orderID) == true)
                {
                    Console.WriteLine("请输入增减数量：");
                    int num = int.Parse(Console.ReadLine());
                            Console.WriteLine("modify");
                            if (choice == 1)
                                {
                                
                                    service.OrderList[orderID].goodsNumber+=num;
                                    service.OrderList[orderID].price = service.OrderList[orderID].goodsNumber * service.OrderList[orderID].goods.price;
                                }
                                else {
                                    if (service.OrderList[orderID].goodsNumber <= num)
                                    {
                                        Console.WriteLine("商品不足");
                                    }
                                    else {
                                    service.OrderList[orderID].goodsNumber -= num;
                                    service.OrderList[orderID].price = service.OrderList[orderID].goodsNumber * service.OrderList[orderID].goods.price;
                                    }
                                }
                                
                            
                        
                }
            }



            public void display() {
                foreach (OrderDetails a in OrderList.Values) {
                    Console.WriteLine(a.ToString());
                }
            }

            public void export() {

                foreach (OrderDetails a in orderDetailsList)
                {
                    Console.WriteLine(a);
                }
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<OrderDetails>));
                
                using (FileStream fs = new FileStream("OrderDetails.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    xmlSerializer.Serialize(fs, orderDetailsList);
                }
                
                Console.WriteLine(File.ReadAllText("OrderDetails.xml"));
            
            
            }
            public void import() {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<OrderDetails>));
                using (FileStream fs = new FileStream("OrderDetails.xml", FileMode.Open, FileAccess.Read))
                {
                    orderDetailsList = (List<OrderDetails>)xmlSerializer.Deserialize(fs);
                }
            }


        }
        //class Client
        //{
        //    protected string telephone;
        //    public string name { get; }
        //    public Client(string username, string phonenumber)
        //    {
        //        this.telephone = phonenumber;
        //        this.name = username;
        //    }

        //    public override string ToString()
        //    {
        //        return $"用户名:{this.name}\n电话号码:{this.telephone}\n";
        //    }
        //}
        [Serializable]
        public class Goods
        {

            public string goodsId { get; set; }
            public string goodsName { get; set; }
            public string goodsType { get; set; }
            public int price { get; set; }
            public Goods(string goodsId, string goodsName, string goodsType, int price)
            {
                this.goodsId = goodsId;
                this.goodsName = goodsName;
                this.goodsType = goodsType;
                this.price = price;
            }
            public Goods() { }
            public override string ToString()
            {
                return $"货物ID: {this.goodsId}, 商品名:{this.goodsName}, 商品类型:{this.goodsType}, 价格:{this.price}\n";
            }


        }
        public static void Main(string[] args)
        {
            Order.addGoods("0001", "T恤衫", "衣服", 120);
            Order.addGoods("0002", "打孔器", "办公用品", 12);
            Order.addGoods("0003", "运动T恤", "衣服", 189);
            Order.addGoods("0004", "菜刀", "厨房用具", 28);
            Order.addGoods("0005", "高压锅", "厨房用具", 130);
            Order.addGoods("0006", "叉子", "厨房用具", 10);
            Order.addGoods("0007", "擦菜板", "厨房用具", 13);
            Order.addGoods("0008", "圆珠笔", "办公用品", 3);
            // 展示货物信息
            service.import();
            foreach (OrderDetails od in orderDetailsList) {
                service.addOrder(od.orderID, od);
            }


            while (true)
            {
                
                Console.WriteLine("\n*********************货物信息**********************");
                foreach (Goods i in goods)
                {
                    Console.Write(i.ToString());
                }
                Console.WriteLine("*********************服务选择**********************");
                Console.WriteLine("1.添加订单  2.删除订单  3.查询订单  4.修改订单  5.退出");
                Console.Write("你的选择: ");
                int choice = int.Parse(Console.ReadLine());
                if (choice == 1)
                {
                    Console.WriteLine("请输入货物ID");
                    int GoodID = int.Parse(Console.ReadLine());
                    Console.WriteLine("店名：");
                    string shopName = Console.ReadLine();
                    Console.WriteLine("商品数量：");
                    int goodsNumber = int.Parse(Console.ReadLine());
                    Console.WriteLine("经手人：");
                    string Name = Console.ReadLine();
                    if (GoodID >= 1 && GoodID <= 8)
                    {
                        Random random = new Random();
                        int orderID = random.Next(100);
                        OrderDetails orderDetails = new OrderDetails(orderID, shopName, goodsNumber, Name, goods[GoodID-1]);
                        service.addOrder(orderID, orderDetails);
                        orderDetailsList.Add(orderDetails);
                        service.export();
                        Console.WriteLine(orderDetailsList.Count);
                    }
                    else
                    {
                        Console.WriteLine("输的什么玩意");
                    }
                    
                }
                if (choice == 2)
                {
                    service.display();
                    Console.WriteLine("请输入要删除的货物ID：");
                    int orderID = int.Parse(Console.ReadLine());
                    service.removeOrder(orderID);
                }
                if (choice == 3)
                {
                    Console.WriteLine("-----------------查询方式---------------------");
                    Console.WriteLine("1.订单号、2.商品名称、3.客户、4.订单金额");
                    int method = int.Parse(Console.ReadLine());

                    switch (method)
                    {
                        case 1:
                            service.display();
                            Console.WriteLine("请输入订单号：");
                            int orderID = int.Parse(Console.ReadLine());
                            service.findOrder(0, orderID);
                            break;
                        case 2:
                            service.display();
                            Console.WriteLine("请输入商品名称：");
                            string goodName = Console.ReadLine();
                            service.findOrder(0, goodName);
                            break;
                        case 3:
                            service.display();
                            Console.WriteLine("请输入客户名：");
                            string userName = Console.ReadLine();
                            service.findOrder(1, userName);
                            break;
                        case 4:
                            service.display();
                            Console.WriteLine("请输入订单金额：");
                            int price = int.Parse(Console.ReadLine());
                            service.findOrder(1, price);
                            break;
                        default: break;
                    }
                }
                if (choice == 4)
                {
                    service.display();
                    Console.WriteLine("请输入订单号: ");
                    int orderID = int.Parse(Console.ReadLine());
                    service.modifyOrder(orderID);
                }
                if (choice == 5) {
                    Console.WriteLine("感谢使用");
                    break;
                }
            }
        }
    };
}
